package com.lianxian.xindong.home.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.flyco.tablayout.SlidingTabLayout;
import com.hxb.library.base.BaseLazyLoadFragment;
import com.hxb.library.base.MessageEvent;
import com.hxb.library.di.component.AppComponent;
import com.hxb.library.integration.EventBusManager;
import com.hxb.library.utils.AntiShakeUtils;
import com.hxb.library.utils.HxbUtils;
import com.hxb.library.utils.LogUtils;
import com.lianxian.xindong.commonres.adapter.MyPagerTitleAdapter;
import com.lianxian.xindong.commonres.fragment.EmptyFragment;
import com.lianxian.xindong.commonsdk.core.EventBusHub;
import com.lianxian.xindong.commonsdk.core.RouterHub;
import com.lianxian.xindong.commonsdk.utils.ImmersionBarUtil;
import com.lianxian.xindong.commonservice.life.service.LifeMainTableService;
import com.lianxian.xindong.home.R;
import com.lianxian.xindong.home.R2;
import com.lianxian.xindong.home.di.component.DaggerHomeComponent;
import com.lianxian.xindong.home.mvp.contract.HomeContract;
import com.lianxian.xindong.home.mvp.presenter.HomePresenter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

import static com.hxb.library.utils.Preconditions.checkNotNull;


/**
 * @作者：hexingbo
 * @时间：06/30/2023
 * @描述：
 */
public class HomeFragment extends BaseLazyLoadFragment<HomePresenter> implements HomeContract.View {

    MyPagerTitleAdapter mAdapter;

    @BindView(R2.id.tab_menu)
    SlidingTabLayout tabMenu;
    @BindView(R2.id.vp_content)
    ViewPager vpContent;
    @BindView(R2.id.iv_camera)
    ImageView ivCamera;
    @BindView(R2.id.iv_filter)
    ImageView ivFilter;

    @Autowired(name = RouterHub.LIFE_SERVICE_LIFEINFOSERVICE)
    LifeMainTableService mLifeMainTableService;

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerHomeComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        HxbUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        HxbUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Override
    public FragmentManager getSupportFragmentManager() {
        return getChildFragmentManager();
    }

    @Override
    public String[] getTitles() {
        return getResources().getStringArray(R.array.home_top_table_values);
    }

    @Override
    public ArrayList<Fragment> getFragments() {
        ArrayList<Fragment> mFragments = new ArrayList<>();
        mFragments.add(VideoFragment.newInstance());
        mFragments.add(CircleFragment.newInstance());
        mFragments.add(EmptyFragment.newInstance());
        mFragments.add(EmptyFragment.newInstance());
        mFragments.add(getLifeMainTableFragment());
        return mFragments;
    }

    /**
     * 本地生活
     */
    private Fragment getLifeMainTableFragment() {
        //当非集成调试阶段, 宿主 App 由于没有依赖其他组件, 所以使用不了对应组件提供的服务
        if (mLifeMainTableService == null) {
            LogUtils.debugInfo("mLifeMainTableService is null");
            return EmptyFragment.newInstance();
        }
        return mLifeMainTableService.getLifeMainTableFragment().getTableFragment();
    }

    @Override
    protected void lazyLoadData() {
        MyPagerTitleAdapter mAdapter = new MyPagerTitleAdapter(getSupportFragmentManager(), getTitles(), getFragments());
        vpContent.setAdapter(mAdapter);
        vpContent.setOffscreenPageLimit(getTitles().length);
        vpContent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int position) {
                changeBarColor(position);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        tabMenu.setViewPager(vpContent);
        tabMenu.onPageSelected(0);
        changeBarColor(0);
    }

    /**
     * 改变标题栏颜色
     *
     * @param position
     */
    private void changeBarColor(int position) {
        LogUtils.debugInfo("当前tabl:" + position);
        if (position != 0) {
            ivCamera.setVisibility(View.INVISIBLE);
            ivFilter.setVisibility(View.INVISIBLE);
            ImmersionBarUtil.setImmersionBar(getActivity(), true);
            tabMenu.setTextSelectColor(getResources().getColor(R.color.public_color_black));
            tabMenu.setTextUnselectColor(getResources().getColor(R.color.public_color_gray));
        } else {
            ivCamera.setVisibility(View.VISIBLE);
            ivFilter.setVisibility(View.VISIBLE);
            ImmersionBarUtil.setImmersionBar(getActivity(), false);
            tabMenu.setTextSelectColor(getResources().getColor(R.color.public_color_white));
            tabMenu.setTextUnselectColor(getResources().getColor(R.color.public_color_white));
        }
        if (position != 0) {
            EventBusManager.getInstance().post(new MessageEvent(EventBusHub.EVENT_SELFIEFRAGMENT_VIDEOPLAYPAUSE));
        } else {
            EventBusManager.getInstance().post(new MessageEvent(EventBusHub.EVENT_SELFIEFRAGMENT_VIDEOPLAYSTART));
        }

    }

    @OnClick({R2.id.iv_camera, R2.id.iv_filter})
    public void onViewClicked(View view) {
        if (AntiShakeUtils.isInvalidClick(view)) {
            return;
        }
        if (view.getId() == R.id.iv_camera) {

        } else if (view.getId() == R.id.iv_filter) {

        }
    }


    @Override
    protected void onMessageEvent(MessageEvent message) {
        super.onMessageEvent(message);
        if (message.getType().equals(EventBusHub.EVENT_HOMEFRAGMENT_CHANGEBARCOLOR)) {
            changeBarColor(vpContent.getCurrentItem());
        }
    }
}
